#include <string.h>

#include "rtc.h"
#include "i2c.h"

#include "../avrx/avrx.h"

#define RTC_ADRESS 	0xD0 //11010000

static TimerControlBlock RTC_Timer;		// The Timer queue
static TimerMessageBlock RTC_TimerMassage;        // TimerMessage timer (just a timer with a
static MessageQueue 	 RTC_Queue;    		// The message queue

typedef struct RTC_MessageControl                 // Declare a new message type with data
{
    MessageControlBlock mcb;
    unsigned char       mode;
	RTC_t 				time;	
}
RTC_Message_t;

static RTC_Message_t RTC_Message;

static RTC_t	ClockTime;

static void 		 SetClockTime(RTC_t time);
static RTC_t 		 GetClockTime(void);
static unsigned char RTC_Write(RTC_t *time);
static unsigned char RTC_Read(RTC_t  *time);


void RTC_GetClockTime(RTC_t *time)
{
    *time = GetClockTime();
}

void RTC_SetClockTime(RTC_t *time)
{
	RTC_Message.mode = RTC_SET_TIME;
	
	RTC_Message.time = *time;
   
    AvrXSendMessage(&RTC_Queue, &RTC_Message.mcb);	

	AvrXWaitMessageAck(&RTC_Message.mcb);

}


static void SetClockTime(RTC_t time)
{
	ClockTime = time;
}

static RTC_t GetClockTime(void)
{
	return ClockTime;
}


static unsigned char RTC_Write(RTC_t *time)
{
	unsigned char pData[7];
	
	pData[0] = ((time->seconds/10) << 4) + (time->seconds%10);
	pData[1] = ((time->minutes/10) << 4) + (time->minutes%10);
	pData[2] = ((time->hour/10) << 4) + (time->hour%10);
	pData[3] = time->day;
	pData[4] = ((time->date/10) << 4) + (time->date%10);
	pData[5] = ((time->month/10) << 4) + (time->month%10);
	pData[6] = ((time->year/10) << 4) + (time->year%10);
			
	i2cMasterSend(RTC_ADRESS, sizeof(pData), (unsigned char *)&pData);

    return i2cGetError();
}

static unsigned char RTC_Read(RTC_t * time)
{
	static unsigned char pRegAddr = 0x00;
    static unsigned char pData[7];  

	i2cMasterTransferNI(RTC_ADRESS
				,1, (unsigned char *)&pRegAddr
				,sizeof(pData), (unsigned char *)&pData);

	if( i2cGetError() == I2C_NONE )
	{				
		time->year = (pData[6] >> 4)*10 + (pData[6] & 0x0F);
		time->month = (pData[5] >> 4)*10 + (pData[5] & 0x0F);
		time->date = (pData[4] >> 4)*10 + (pData[4] & 0x0F);
		time->day = pData[3] & 0x07;
		time->hour = (pData[2] >> 4)*10 + (pData[2] & 0x0F);
		time->minutes = (pData[1] >> 4)*10 + (pData[1] & 0x0F);
		time->seconds = (pData[0] >> 4)*10 + (pData[0] & 0x0F);
		
		return TRUE;
	}
	
	return FALSE;
}


AVRX_GCC_TASKDEF(RTC_Task, 35, 4)
{
    RTC_Message_t *p;
	RTC_t 				 time_t;
	
	i2cInit();

	AvrXDelay(&RTC_Timer, 10);        //   Delay 10ms

	while(1)
	{
	
		AvrXStartTimerMessage(&RTC_TimerMassage, 500, &RTC_Queue);
	
		p = (RTC_Message_t *)AvrXWaitMessage(&RTC_Queue);
        
		if(p == &RTC_Message)
		{		
            if(p->mode == RTC_SET_TIME)
            {
                RTC_Write( &p->time );
            }
			AvrXAckMessage( &p->mcb );
		}
		else if((TimerMessageBlock *)p == &RTC_TimerMassage)
		{
			if( RTC_Read(&time_t) == TRUE )
			{
				SetClockTime(time_t);
			}
		}
        else
        {
            AvrXHalt();
        }	
	}
}
